<?php

class m_users_documents_GroupBase extends f_document_BaseDocument
{

	private $rights = array();
	private $user = array();
	private $group = array();

	public function __construct($id = null, $lang = null)
	{
		parent::__construct($id, null, $lang);
	}

	public function getTableName()
	{
		return "m_users__group";
	}

	public function getDocumentModel()
	{
		return "modules_users/group";
	}
	
	protected function getFieldsBag()
	{
		return array('document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_publicationstatus', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label', 			'rights', 			'user', 			'group');
	}

	public function getdefaultPublicationstatus()
	{
		return "PUBLISHED";
	}

	/**
	 * @return boolean
	 */
	public function isRevisable()
	{
		return false;
	}
	
	/**
	 * @return boolean
	 */
	public function isLocalized()
	{
		return false;
	}	
	
    /**
	 * @return boolean
	 */
	public function isRecursive()
	{
		return $this->isRecursive;
	}
	
	/**
	 * @param boolean $value
	 * @return m_users_documents_GroupBase
	 */	
	public function setIsRecursive($value)
	{
		$this->isRecursive = $value;
	}
		
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_users_documents_Group
	 */
	public function addRightsById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addRights($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_users_documents_Group
	 */
	public function addRights($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_rights_documents_Rights)
		{
			if(!is_array($this->rights))
			{
				$this->rights = array();
			}
		
			if(!in_array($document, $this->rights))
			{
				if(is_null($orderNumber))
				{
					$this->rights[] = $document;
				}
				else
				{
					$this->rights[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_rights_documents_Rights)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_rights_documents_Rights
	 */
	public function getRights()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "rights", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getRightsNb()
	{
		return $this->rights;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setRights($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->rights = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'rights', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->rights = intval($nbRelations);
		}
	}
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_users_documents_Group
	 */
	public function addUserById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addUser($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_users_documents_Group
	 */
	public function addUser($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_users_documents_User)
		{
			if(!is_array($this->user))
			{
				$this->user = array();
			}
		
			if(!in_array($document, $this->user))
			{
				if(is_null($orderNumber))
				{
					$this->user[] = $document;
				}
				else
				{
					$this->user[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_users_documents_User)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_users_documents_User
	 */
	public function getUser()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "user", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getUserNb()
	{
		return $this->user;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setUser($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->user = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'user', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->user = intval($nbRelations);
		}
	}
	/**
	 * Add a new related document by document_uid	 
	 * 
	 * @return m_users_documents_Group
	 */
	public function addGroupById($documentId, $orderNumber = null)
	{
		$document = f_document_Provider::getInstance()->getByUniqueId($documentId);
		return $this->addGroup($document, $orderNumber);
	}

	/**
	 * Add a new related document
	 * 
	 * @return m_users_documents_Group
	 */
	public function addGroup($document, $orderNumber = null)
	{		
		if(is_object($document) && $document instanceof m_users_documents_Group)
		{
			if(!is_array($this->group))
			{
				$this->group = array();
			}
		
			if(!in_array($document, $this->group))
			{
				if(is_null($orderNumber))
				{
					$this->group[] = $document;
				}
				else
				{
					$this->group[$orderNumber] = $document;
				}
			}
		}
		else
		{
			throw new Exception("Document is not a valid object or from valid model(m_users_documents_Group)");	
		}
		return $this;
	}
	
	/**
	 * Get the related documents in array
	 *
	 * @return array m_users_documents_Group
	 */
	public function getGroup()
	{
		$relationManager = f_relation_Manager::getInstance();
		return $relationManager->getChilds($this, "group", $this->getLang());
	}
	
	/**
	 * Get related documents number
	 * 
	 * @return integer
	 */
	public function getGroupNb()
	{
		return $this->group;
	}		
	
	/**
	 * Set the number of related documents
	 * Only used when saving or initialize document
	 */
	protected function setGroup($value)
	{
		if(!is_null($value) && is_int($value))
		{
			$this->group = $value;
		}
		else
		{
			$dbProvider = f_database_Provider::getInstance();
	
			$sql = "SELECT COUNT(`child_id`) FROM `f_relation` WHERE `relation_type`=:relationType AND `lang`=:lang AND `parent_id`=:parentId";
			
			$nbRelations = $dbProvider->setQuery($sql)->execute(array("relationType" => 'group', "lang" => $this->getLang(), "parentId" => $this->getUniqueId()))->getQueryResult(PDO::FETCH_COLUMN);
	
			$this->group = intval($nbRelations);
		}
	}
	public function relationFields()
	{
		return array_merge(array('rights', 'user', 'group'),parent::relationFields());
	}



}